package com.yicong.ants.ui.coin;


import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SizeUtils;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.fragment.SimpleLazyFragment;
import com.yicong.ants.R;
import com.yicong.ants.activity.DoubleConfirmActivity;
import com.yicong.ants.bean.YcAccountInfo;
import com.yicong.ants.bean.coin.TransInfo;
import com.yicong.ants.databinding.FragmentYcAccountBinding;
import com.yicong.ants.manager.BusinessHelper;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.config.ZzConfigs;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.ui.me.PayPwdSettingActivity;
import com.zx.sdk.util.ToastHelper;

import java.util.Collections;
import java.util.List;

/**
 * yc账户
 */
public class YcAccountFragment extends SimpleLazyFragment<FragmentYcAccountBinding> implements View.OnClickListener {

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_yc_account;
    }

    YcAccountInfo mData;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mDataBind.setClick(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        onLoadData();
    }

    @Override
    protected void onLoadData() {
        showProgress();
        addSubscribe(RetrofitHelper.getApis().accountInfo(Collections.emptyMap())
                .compose(RxHelper.toMain())
                .subscribe(respBean -> {
                    hideProgress();
                    if (respBean.isCodeFail()) {
                        showToast(respBean.getMsg());
                        return;
                    }
                    mData = respBean.getData();
                    updateInfo();
                }, RxHelper.getHideProgressConsumer(this)));
    }

    //设置温馨提示
    private void setTips() {
        mDataBind.content1.removeAllViews();
        List<String> exchangeNotice = mData.getYc_description();
        for (int i = 0; i < exchangeNotice.size(); i++) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(requireContext()).inflate(R.layout.item_yc_account, mDataBind.content1, false);
            int padding = SizeUtils.dp2px(5);
            view.setPadding(padding, padding, padding, 0);
            TextView textView = view.findViewById(R.id.text);
            textView.setText(exchangeNotice.get(i));
            textView.setTextSize(13);
            textView.setTextColor(ContextCompat.getColor(requireContext(), R.color.new_text_color_1));
            mDataBind.content1.addView(view, layoutParams);
        }

        mDataBind.item1.removeAllViews();
        mDataBind.item2.removeAllViews();
        List<YcAccountInfo.YcTaxLevel> ycTaxLevels = mData.getYc_tax_level();
        for (int i = 0; i < ycTaxLevels.size(); i++) {
            for (int j = 0; j < 2; j++) {
                int height;
                Typeface typeface;
                if (i == 0) {
                    typeface = Typeface.DEFAULT_BOLD;
                    height = SizeUtils.dp2px(44);
                } else {
                    typeface = Typeface.DEFAULT;
                    height = SizeUtils.dp2px(34);
                }
                LinearLayout.LayoutParams txLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
                TextView textView = new TextView(requireContext());
                textView.setTextSize(13);
                textView.setTypeface(typeface);
                if (j == 0) {
                    textView.setText(ycTaxLevels.get(i).getEnergy());
                } else {
                    textView.setText(ycTaxLevels.get(i).getTax());
                }
                textView.setGravity(Gravity.CENTER);
                textView.setTextColor(ContextCompat.getColor(requireContext(), R.color.new_text_color_1));
                if (j == 0) {
                    mDataBind.item1.addView(textView, txLayoutParams);
                } else {
                    mDataBind.item2.addView(textView, txLayoutParams);
                }
            }
        }
    }

    private void updateInfo() {
        mDataBind.setBean(mData);
        setTips();
    }

    @Override
    public void onFirstUserVisible() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.yc_field) {
            //yc明细
            Router.turnTo(mContext, YcRecordActivity.class).putExtra("total", mData.getYc()).start();
        } else if (id == R.id.stock_field) {
            //yc仓库明细
            Router.turnTo(mContext, YcStockRecordActivity.class).putExtra("total", mData.getYc_stock()).start();
        } else if (id == R.id.trans_in || id == R.id.trans_out) {
            if (mData == null) {
                ToastHelper.showToast("数据错误，请刷新重试");
                return;
            }
            if ("0".equals(mData.getHave_pay_password())) {
                UiHelper.showConfirmActionDialog(mContext, "您还没有设置支付密码，是否前往设置"
                        , (dialog, which) -> {
                            //
                            Router.turnTo(mContext, PayPwdSettingActivity.class).start();
                            requireActivity().finish();
                        });
                return;
            }
            int type = v.getId() == R.id.trans_in ? 10 : 20;
            Dialogs.showYcStockTran(mContext, TransInfo.instance(mData), type, (amount, payPwd) -> {
                //
                BusinessHelper.onYcStockTrans((UserCoinActivity) requireActivity(), type, amount, payPwd, () -> {
                    //
                    onLoadData();
                });
            });
        } else if (id == R.id.pickup_center) {
            //跳转提货中心
            ZzConfigs.toWebActivityByUrlNoTitle(mContext, "yc_pickup_center");
        } else if (id == R.id.integral_detail) {
            //积分明细
            ZzConfigs.toWebActivityByUrlNotLogin(mContext, "yc_unit_record");
        }
    }
}
